from fastapi import FastAPI, WebSocket
from fastapi.websockets import WebSocketDisconnect
from typing import List

app = FastAPI()

# 用于存储连接到 /ws/client/deploy 的客户端
deploy_clients: List[WebSocket] = []


@app.websocket("/ws/client/deploy")
async def deploy_websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    deploy_clients.append(websocket)
    try:
        while True:
            # 这里可以处理 /ws/client/deploy 的数据
            data = await websocket.receive_text()
            await websocket.send_text(f"Deploy message received: {data}")
    except WebSocketDisconnect:
        deploy_clients.remove(websocket)


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    try:
        while True:
            data = await websocket.receive_text()
            await websocket.send_text(f"Message text was: {data}")

            # 触发 /ws/client/deploy 的客户端
            for client in deploy_clients:
                await client.send_text(f"Triggered from /ws: {data}")
    except WebSocketDisconnect:
        pass
